//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "AffineModel.h"
using namespace Cephei::QL::Models;
#include <gen/QL/Math/Array.h>
using namespace Cephei::QL::Math;
#define HANDLE
#define ABSTRACT
#undef STRUCT
Cephei::QL::Models::CAffineModel::CAffineModel (boost::shared_ptr<QuantLib::AffineModel>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phAffineModel = NULL;
#endif
	_ppAffineModel = &childNative;
    
}
Cephei::QL::Models::CAffineModel::CAffineModel (QuantLib::AffineModel& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phAffineModel = NULL;
#endif
	_ppAffineModel = new boost::shared_ptr<QuantLib::AffineModel> (&childNative);
    
    _AffineModelOwner = owner;
    
}

Cephei::QL::Models::CAffineModel::CAffineModel (CAffineModel^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phAffineModel = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppAffineModel = new boost::shared_ptr<QuantLib::AffineModel> (copy->GetShared());
        
    }
}
Cephei::QL::Models::CAffineModel::CAffineModel (System::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phAffineModel = NULL;
#endif
	if (!t->IsSubclassOf(CAffineModel::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Models::CAffineModel::CAffineModel (QuantLib::Handle<QuantLib::AffineModel>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phAffineModel = &childNative;
	_ppAffineModel = &static_cast<boost::shared_ptr<QuantLib::AffineModel>>(childNative.currentLink());
    
    _AffineModelOwner = owner;
}
Cephei::QL::Models::CAffineModel::CAffineModel (QuantLib::Handle<QuantLib::AffineModel> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phAffineModel = &childNative;
	_ppAffineModel = &static_cast<boost::shared_ptr<QuantLib::AffineModel>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Models::CAffineModel::CAffineModel (QuantLib::AffineModel childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phAffineModel = NULL;
#endif
	_ppAffineModel = new boost::shared_ptr<QuantLib::AffineModel> (new QuantLib::AffineModel (childNative));
    
}
#endif

Cephei::QL::Models::CAffineModel::~CAffineModel ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppAffineModel != NULL)
    {
	    delete _ppAffineModel;
        _ppAffineModel = NULL;
    }
}
Cephei::QL::Models::CAffineModel::!CAffineModel ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppAffineModel != NULL)
    {
	    delete _ppAffineModel;
    }
}
QuantLib::AffineModel& Cephei::QL::Models::CAffineModel::GetReference ()
{
    if (_ppAffineModel == NULL) throw gcnew NativeNullException ();
	return **_ppAffineModel;
}
boost::shared_ptr<QuantLib::AffineModel>& Cephei::QL::Models::CAffineModel::GetShared ()
{
    if (_ppAffineModel == NULL) throw gcnew NativeNullException ();
	return *_ppAffineModel;
}
QuantLib::AffineModel* Cephei::QL::Models::CAffineModel::GetPointer ()
{
    if (_ppAffineModel == NULL) throw gcnew NativeNullException ();
	return &**_ppAffineModel;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::AffineModel>& Cephei::QL::Models::CAffineModel::GetHandle ()
{
	if (_phAffineModel == NULL)
	{
		_phAffineModel = new Handle<QuantLib::AffineModel> (*_ppAffineModel);
	}
	return *_phAffineModel;
}
#endif
bool Cephei::QL::Models::CAffineModel::HasNative () 
{
	return (_ppAffineModel != NULL);
}

Double Cephei::QL::Models::CAffineModel::Discount (Double t)
{
    try
    {
        QuantLib::Time _t = (QuantLib::Time)ValueHelper::Convert (t);
    	QuantLib::DiscountFactor _rv = (QuantLib::DiscountFactor)(*_ppAffineModel)->discount ( _t );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Models::CAffineModel::DiscountBond (Double now, Double maturity, Cephei::QL::Math::IArray^ factors)
{
    CArray^ _Cfactors;
    try
    {
        QuantLib::Time _now = (QuantLib::Time)ValueHelper::Convert (now);
        QuantLib::Time _maturity = (QuantLib::Time)ValueHelper::Convert (maturity);
        _Cfactors = safe_cast<CArray^> (factors);
        _Cfactors->Lock();
        QuantLib::Array _factors = static_cast<QuantLib::Array> (_Cfactors->GetReference ()); 
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppAffineModel)->discountBond ( _now,  _maturity,  _factors );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cfactors != nullptr) _Cfactors->Unlock();
    }
}
Double Cephei::QL::Models::CAffineModel::DiscountBondOption (QL::Option::TypeEnum type, Double strike, Double maturity, Double bondMaturity)
{
    try
    {
        QuantLib::Option::Type _type = (QuantLib::Option::Type)type ;
        QuantLib::Real _strike = (QuantLib::Real)ValueHelper::Convert (strike);
        QuantLib::Time _maturity = (QuantLib::Time)ValueHelper::Convert (maturity);
        QuantLib::Time _bondMaturity = (QuantLib::Time)ValueHelper::Convert (bondMaturity);
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppAffineModel)->discountBondOption ( _type,  _strike,  _maturity,  _bondMaturity );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

